/*
 ******************************************************************************
 *
 *       Copyright ASTRI, 2016.  All rights reserved.
 *
 *   This software is proprietary to and embodies the confidential technology
 *   of Hong Kong Applied Science and Technology Research Institute Company
 *   Limited (ASTRI).
 *   Possession, use, or copying of this software and media is authorized
 *   only pursuant to a valid written license from ASTRI or an authorized
 *   sublicensor.
 *
 * File Name:
 *   XXX.java
 *
 * Purpose:
 *   
 *   
 *   
 *
 * Project or Modules Name:
 *   
 *
 * Modules Contained:
 *   N/A
 *
 * Modification History:
 * Date(DDMMYY)  Version No Modified By  Description
 * ------------  ---------- -----------  -----------
 * 20160405       0.0.0.1    XXX     Created
 *
 ******************************************************************************
 */


package com.j.thetabk.thetas;

import org.json.JSONException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * Created by user on 14/4/2016.
 */
public class ThetaSConnectorF extends ThetaSConnectorE {

    public static ThetaSDoc doc=new ThetaSDoc();


    public ThetaSConnectorF(String cameraIpAddress) {
        super(cameraIpAddress);
    }

    public void SaveSettingSD() throws IOException
    {
        SaveSettingSD(doc.sDir,doc.settingName+doc.settingExtension);
    }

    public void SaveSettingSD(String folder, String fileName) throws IOException
    {
        String seg="\r\n";

        File destDir = new File(folder);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        File file = new File(folder+"/"+fileName);
        FileOutputStream fos = new FileOutputStream(file,false);

        fos.write(String.valueOf(properties[4].choice).getBytes());
        fos.write(seg.getBytes());
        fos.write(String.valueOf(properties[5].choice).getBytes());
        fos.write(seg.getBytes());

        for(int x=0;x<4;x++)
        {
            for(int i=0;i<properties[x].selection.length;i++)
            {
                fos.write(String.valueOf(properties[x].selection[i]?1:0).getBytes());
            }
            fos.write(seg.getBytes());
        }

        fos.close();
    }

    public boolean LoadSettingSD(String fileName) throws IOException, JSONException {
        BufferedReader reader= new BufferedReader(new FileReader(new File(fileName)));

        String tempString;
        int tmpchoice;
        tempString = reader.readLine();
        if(tempString==null)
            return false;
        tmpchoice=Integer.parseInt(tempString);

        if(properties[4].choice != tmpchoice)
        {
            properties[4].choice = tmpchoice;
            SetProperty(4);
        }

        tempString = reader.readLine();
        if(tempString==null)
            return false;
        tmpchoice=Integer.parseInt(tempString);

        if(properties[4].choice==0 && properties[5].choice != tmpchoice)
        {
            properties[5].choice = tmpchoice;
            SetProperty(5);
        }


        for(int i=0;i<4;i++)
        {
            tempString = reader.readLine();
            if (tempString == null)
                return false;

            for(int j=0;j<tempString.length() && j<properties[i].selection.length;j++)
                properties[i].selection[j]=(tempString.charAt(j)=='1');

            tmpchoice=properties[i].FirstTrue();

            if(tmpchoice==-2)
            {
                properties[i].SyncSelection();
                continue;
            }
            properties[i].TrueCount();
            if(properties[i].choice!=tmpchoice)
            {
                properties[i].choice=tmpchoice;
                if(tmpchoice>=0)
                    SetProperty(i);
            }
        }

        reader.close();
        return true;
    }

    public void TakePhotoBK(ThetaSBKEventListener listener, int sleepms) throws InterruptedException, IOException, JSONException {

        ArrayList<Integer> propertyIdx=new ArrayList<Integer>();
        ArrayList<Integer> settingIdx=new ArrayList<Integer>();
        BracketList(settingIdx, propertyIdx);

        String []files=null;
        if(FirmwareVersionNumber()<1.42)
        {
            SelfTimerCountDown(properties[12].choice,listener);
            files=TakePhotoBK(settingIdx,propertyIdx,listener,sleepms);
        }
        else
        {
            files=TakePhotoBKWithDelay(settingIdx,propertyIdx,listener,sleepms);
        }

        listener.onComplete();
        if(BracketNum()>1 || numbers[2].value>1)
        {
            String fileName = new SimpleDateFormat("yyyyMMdd_hhmmss").format(new java.util.Date());
            fileName = fileName + ".txt";
            doc.SaveFileListSD(fileName, files);
            listener.onSaveImageList(fileName);
        }

    }

}
